<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Categories &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Predicates" href="predicates.html" />
    <link rel="prev" title="Protocols" href="protocols.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="declarative.html">Declarative object-oriented programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="features.html">Main features</a></li>
<li class="toctree-l2"><a class="reference internal" href="nomenclature.html">Nomenclature</a></li>
<li class="toctree-l2"><a class="reference internal" href="messages.html">Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="objects.html">Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="protocols.html">Protocols</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Categories</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#defining-a-new-category">Defining a new category</a></li>
<li class="toctree-l3"><a class="reference internal" href="#hot-patching">Hot patching</a></li>
<li class="toctree-l3"><a class="reference internal" href="#finding-defined-categories">Finding defined categories</a></li>
<li class="toctree-l3"><a class="reference internal" href="#creating-a-new-category-at-runtime">Creating a new category at runtime</a></li>
<li class="toctree-l3"><a class="reference internal" href="#abolishing-an-existing-category">Abolishing an existing category</a></li>
<li class="toctree-l3"><a class="reference internal" href="#category-directives">Category directives</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#dynamic-categories">Dynamic categories</a></li>
<li class="toctree-l4"><a class="reference internal" href="#category-documentation">Category documentation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#loading-files-into-a-category">Loading files into a category</a></li>
<li class="toctree-l4"><a class="reference internal" href="#declaring-object-aliases">Declaring object aliases</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#category-relationships">Category relationships</a></li>
<li class="toctree-l3"><a class="reference internal" href="#category-properties">Category properties</a></li>
<li class="toctree-l3"><a class="reference internal" href="#importing-categories">Importing categories</a></li>
<li class="toctree-l3"><a class="reference internal" href="#calling-category-predicates">Calling category predicates</a></li>
<li class="toctree-l3"><a class="reference internal" href="#parametric-categories">Parametric categories</a></li>
<li class="toctree-l3"><a class="reference internal" href="#built-in-categories">Built-in categories</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#the-built-in-category-core-messages">The built-in category <code class="docutils literal notranslate"><span class="pre">core_messages</span></code></a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#multi-threading-applications">Multi-threading applications</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html">Predicates</a></li>
<li class="toctree-l2"><a class="reference internal" href="inheritance.html">Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="events.html">Event-driven programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="threads.html">Multi-threading programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="errors.html">Error handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="reflection.html">Reflection</a></li>
<li class="toctree-l2"><a class="reference internal" href="programming.html">Writing and running applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="printing.html">Printing messages and asking questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="expansion.html">Term and goal expansion</a></li>
<li class="toctree-l2"><a class="reference internal" href="documenting.html">Documenting</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance.html">Performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="installing.html">Installing Logtalk</a></li>
<li class="toctree-l2"><a class="reference internal" href="migration.html">Prolog integration and migration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devtools/index.html">Developer Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">User Manual</a></li>
      <li class="breadcrumb-item active">Categories</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/userman/categories.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="categories">
<span id="categories-categories"></span><h1>Categories<a class="headerlink" href="#categories" title="Link to this heading"></a></h1>
<p>Categories are <em>fine-grained units of code reuse</em> and can be regarded as a
dual concept of protocols. Categories provide a way to encapsulate a set
of related predicate declarations and definitions that do not represent
a complete object and that only make sense when composed with other
predicates. Categories may also be used to break a complex object into
functional units. A category can be imported by several objects (without
code duplication), including objects participating in prototype or
class-based hierarchies. This concept of categories shares some ideas
with Smalltalk-80 functional categories <a class="reference internal" href="../bibliography.html#goldberg83" id="id1"><span>[Goldberg83]</span></a>, Flavors mix-ins
<a class="reference internal" href="../bibliography.html#moon86" id="id2"><span>[Moon86]</span></a> (without necessarily implying multi-inheritance), and
Objective-C categories <a class="reference internal" href="../bibliography.html#cox86" id="id3"><span>[Cox86]</span></a>. Categories may also <em>complement</em>
existing objects, thus providing a <a class="reference internal" href="../glossary.html#term-hot-patching"><span class="xref std std-term">hot patching</span></a> mechanism
inspired by the Objective-C categories functionality.</p>
<p>Logtalk defines a built-in category, <a class="reference external" href="../../apis/core_messages_0.html#core-messages-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">core_messages</span></a>,
which is described at the end of this section.</p>
<section id="defining-a-new-category">
<span id="categories-defining"></span><h2>Defining a new category<a class="headerlink" href="#defining-a-new-category" title="Link to this heading"></a></h2>
<p>We can define a new category in the same way we write Prolog code: by
using a text editor. Logtalk source files may contain one or more
objects, categories, or protocols. If you prefer to define each entity
in its own source file, it is recommended that the file be named after
the category. By default, all Logtalk source files use the extension
<code class="docutils literal notranslate"><span class="pre">.lgt</span></code> but this is optional and can be set in the adapter files.
Intermediate Prolog source files (generated by the Logtalk compiler)
have, by default, a <code class="docutils literal notranslate"><span class="pre">_lgt</span></code> suffix and a <code class="docutils literal notranslate"><span class="pre">.pl</span></code> extension. Again, this
can be set to match the needs of a particular Prolog compiler in the
corresponding adapter file. For example, we may define a category named
<code class="docutils literal notranslate"><span class="pre">documenting</span></code> and save it in a <code class="docutils literal notranslate"><span class="pre">documenting.lgt</span></code> source file that
will be compiled to a <code class="docutils literal notranslate"><span class="pre">documenting_lgt.pl</span></code> Prolog file (depending on
the <a class="reference internal" href="../glossary.html#term-backend-Prolog-compiler"><span class="xref std std-term">backend compiler</span></a>, the names of the
intermediate Prolog files may include a directory hash and a process
identifier to prevent file name clashes when embedding Logtalk
applications or running parallel Logtalk processes).</p>
<p>Category names can be atoms or compound terms (when defining parametric
categories). Objects, categories, and protocols share the same name
space: we cannot have a category with the same name as an object or a
protocol.</p>
<p>Category code (directives and predicates) is textually encapsulated by
using two Logtalk directives: <a class="reference internal" href="../refman/directives/category_1_4.html#directives-category-1-4"><span class="std std-ref">category/1-4</span></a> and
<a class="reference internal" href="../refman/directives/end_category_0.html#directives-end-category-0"><span class="std std-ref">end_category/0</span></a>. The
most simple category will be one that is self-contained, not depending
on any other Logtalk entity:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(<span class="nv">Category</span>).
    ...
<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>If a category implements one or more protocols, then the opening
directive will be:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(<span class="nv">Category</span>,
    <span class="k">implements</span>([<span class="nv">Protocol1</span>, <span class="nv">Protocol2</span>, ...])).
    ...
<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>A category may be defined as a composition of other categories by
writing:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(<span class="nv">Category</span>,
    <span class="k">extends</span>([<span class="nv">Category1</span>, <span class="nv">Category2</span>, ...])).
    ...
<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>This feature should only be used when extending a category without
breaking its functional cohesion (for example, when a modified version
of a category is needed for importing on several unrelated objects). The
preferred way of composing several categories is by importing them into
an object. When a category overrides a predicate defined in an extended
category, the overridden definition can still be called by using the
<a class="reference internal" href="../refman/control/call_super_1.html#control-call-super-1"><span class="std std-ref">(^^)/1</span></a> control construct.</p>
<p>Categories cannot inherit from objects. In addition, categories cannot
define clauses for dynamic predicates. This restriction applies because
a category can be imported by several objects and because we cannot use
the database handling built-in methods with categories (messages can
only be sent to objects). A consequence of this restriction is that a
category cannot declare a predicate (or non-terminal) as both multifile
and dynamic. However, categories may contain declarations for dynamic
predicates, and they can contain predicates that handle dynamic predicates.
For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(attributes).

<span class="p">    :- </span><span class="k">public</span>(attribute<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">public</span>(set_attribute<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">public</span>(del_attribute<span class="o">/</span><span class="m">2</span>).

<span class="p">    :- </span><span class="k">private</span>(attribute_<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">dynamic</span>(attribute_<span class="o">/</span><span class="m">2</span>).

    attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% called in the context of &quot;self&quot;</span>
        <span class="o">::</span>attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>).

    set_attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% retract old clauses in &quot;self&quot;</span>
        <span class="o">::</span><span class="k">retractall</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">_</span>)),
        <span class="c">% assert new clause in &quot;self&quot;</span>
        <span class="o">::</span><span class="k">assertz</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>)).

    del_attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% retract clause in &quot;self&quot;</span>
        <span class="o">::</span><span class="k">retract</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>)).

<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>Each object importing this category will have its own <code class="docutils literal notranslate"><span class="pre">attribute_/2</span></code>
private and dynamic predicate. The predicates <code class="docutils literal notranslate"><span class="pre">attribute/2</span></code>,
<code class="docutils literal notranslate"><span class="pre">set_attribute/2</span></code>, and <code class="docutils literal notranslate"><span class="pre">del_attribute/2</span></code> always access and modify
the dynamic predicate contained in the object receiving the
corresponding messages (i.e., <em>self</em>). But it’s also possible to define
predicates that handle dynamic predicates in the context of <em>this</em>
instead of <em>self</em>. For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(attributes).

<span class="p">    :- </span><span class="k">public</span>(attribute<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">public</span>(set_attribute<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">public</span>(del_attribute<span class="o">/</span><span class="m">2</span>).

<span class="p">    :- </span><span class="k">private</span>(attribute_<span class="o">/</span><span class="m">2</span>).
<span class="p">    :- </span><span class="k">dynamic</span>(attribute_<span class="o">/</span><span class="m">2</span>).

    attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% call in the context of &quot;this&quot;</span>
        attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>).

    set_attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% retract old clauses in &quot;this&quot;</span>
        <span class="k">retractall</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">_</span>)),
        <span class="c">% asserts clause in &quot;this&quot;</span>
        <span class="k">assertz</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>)).

    del_attribute(<span class="nv">Attribute</span>, <span class="nv">Value</span>) <span class="o">:-</span>
        <span class="c">% retract clause in &quot;this&quot;</span>
        <span class="k">retract</span>(attribute_(<span class="nv">Attribute</span>, <span class="nv">Value</span>)).

<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>When defining a category that declares and handles dynamic predicates,
working in the context of <em>this</em> ties those dynamic predicates to the
object importing the category, while working in the context of <em>self</em>
allows each object inheriting from the object that imports the category
to have its own set of clauses for those dynamic predicates.</p>
</section>
<section id="hot-patching">
<span id="categories-patching"></span><h2>Hot patching<a class="headerlink" href="#hot-patching" title="Link to this heading"></a></h2>
<p>A category may also explicitly complement one or more existing objects,
thus providing <a class="reference internal" href="../glossary.html#term-hot-patching"><span class="xref std std-term">hot patching</span></a> functionality inspired by Objective-C
categories:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(<span class="nv">Category</span>,
    <span class="k">complements</span>([<span class="nv">Object1</span>, <span class="nv">Object2</span>, ....])).
    ...
<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>This allows us to add missing directives (e.g., to define
<a class="reference internal" href="../glossary.html#term-predicate-alias"><span class="xref std std-term">aliases</span></a> for complemented object predicates),
replace broken predicate definitions, add new predicates, and add protocols
and categories to existing objects without requiring access or modifications
to their source code. Common scenarios are adding logging or debugging
predicates to a set of objects. Complemented objects need to be compiled
with the <a class="reference internal" href="programming.html#flag-complements"><span class="std std-ref">complements</span></a> compiler flag set <code class="docutils literal notranslate"><span class="pre">allow</span></code>
(to allow both patching and adding functionality) or <code class="docutils literal notranslate"><span class="pre">restrict</span></code> (to allow
only adding new functionality). A complementing category takes preference
over a previously loaded complementing category for the same object, thus
allowing patching a previous patch if necessary.</p>
<p>When replacing a predicate definition, it is possible to call the overridden
definition in the object from the new definition in the category by using the
<a class="reference internal" href="../refman/control/call_in_this_1.html#call-in-this-1"><span class="std std-ref">(&#64;)/1</span></a> control construct. This construct is only meaningful
when used within categories and requires a compile-time bound goal argument,
which is called in <a class="reference internal" href="../glossary.html#term-this"><span class="xref std std-term">this</span></a> (i.e., in the context of the complemented
object or the object importing a category). As an example, consider the
following object:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(bird).

<span class="p">    :- </span><span class="k">set_logtalk_flag</span>(complements, allow).

<span class="p">    :- </span><span class="k">public</span>(make_sound<span class="o">/</span><span class="m">0</span>).
    make_sound <span class="o">:-</span>
        <span class="k">write</span>(<span class="s">&#39;Chirp, chirp!&#39;</span>), <span class="k">nl</span>.

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>We can use the <code class="docutils literal notranslate"><span class="pre">(&#64;)/1</span></code> control construct to wrap the original <code class="docutils literal notranslate"><span class="pre">make_sound/0</span></code>
predicate definition by writing:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(logging,
    <span class="k">complements</span>(bird)).

    make_sound <span class="o">:-</span>
        <span class="k">write</span>(<span class="s">&#39;Started making sound...&#39;</span>), <span class="k">nl</span>,
        <span class="o">@</span>make_sound,
        <span class="k">write</span>(<span class="s">&#39;... finished making sound.&#39;</span>), <span class="k">nl</span>.

<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>After loading the object and the category, calling the <code class="docutils literal notranslate"><span class="pre">make_sound/0</span></code>
predicate will result in the following output:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- bird::make_sound.

Started making sound...
Chirp, chirp!
... finished making sound.
yes
</pre></div>
</div>
<p>Note that <a class="reference internal" href="../glossary.html#term-super-call"><span class="xref std std-term">super calls</span></a> from predicates defined in
complementing categories lookup inherited definitions as if the calls
were made from the complemented object instead of the category ancestors.
This allows more comprehensive object patching. But it also means that,
if you want to patch an object so that it imports a category that extends
another category and uses super calls to access the extended category
predicates, you will need to define a (possibly empty) complementing
category that extends the category that you want to add.</p>
<p>An unfortunate consequence of allowing an object to be patched at
runtime using a complementing category is that it disables the use of
<a class="reference internal" href="../glossary.html#term-static-binding"><span class="xref std std-term">static binding</span></a> optimizations for messages sent to the complemented
object, as it can always be later patched, thus rendering the static
binding optimizations invalid.</p>
<p>Another important caveat is that, while a complementing category can
replace a predicate definition, local callers of the replaced predicate
will still call the non-patched version of the predicate. This is a
consequence of the lack of a portable solution at the
<a class="reference internal" href="../glossary.html#term-backend-Prolog-compiler"><span class="xref std std-term">backend Prolog compiler</span></a> level for replacing static predicate
definitions.</p>
</section>
<section id="finding-defined-categories">
<span id="categories-finding"></span><h2>Finding defined categories<a class="headerlink" href="#finding-defined-categories" title="Link to this heading"></a></h2>
<p>We can find, by backtracking, all defined categories by using the
<a class="reference internal" href="../refman/predicates/current_category_1.html#predicates-current-category-1"><span class="std std-ref">current_category/1</span></a> built-in predicate with an
unbound argument:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- current_category(Category).
</pre></div>
</div>
<p>This predicate can also be used to test if a category is defined by
calling it with a valid category identifier (an atom or a compound
term).</p>
</section>
<section id="creating-a-new-category-at-runtime">
<span id="categories-creating"></span><h2>Creating a new category at runtime<a class="headerlink" href="#creating-a-new-category-at-runtime" title="Link to this heading"></a></h2>
<p>A category can be dynamically created at runtime by using the
<a class="reference internal" href="../refman/predicates/create_category_4.html#predicates-create-category-4"><span class="std std-ref">create_category/4</span></a> built-in predicate:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- create_category(Category, Relations, Directives, Clauses).
</pre></div>
</div>
<p>The first argument should be either a variable or the name of the new
category (a Prolog atom, which must not match with an existing entity
name). The remaining three arguments correspond to the relations
described in the opening category directive and to the category code
contents (directives and clauses).</p>
<p>For example, the call:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- create_category(
         ccc,
         [implements(ppp)],
         [private(bar/1)],
         [(foo(X):-bar(X)), bar(1), bar(2)]
     ).
</pre></div>
</div>
<p>is equivalent to compiling and loading the category:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(ccc,
    <span class="k">implements</span>(ppp)).

<span class="p">    :- </span><span class="k">dynamic</span>.

<span class="p">    :- </span><span class="k">private</span>(bar<span class="o">/</span><span class="m">1</span>).

    foo(<span class="nv">X</span>) <span class="o">:-</span>
        bar(<span class="nv">X</span>).

    bar(<span class="m">1</span>).
    bar(<span class="m">2</span>).

<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>If we need to create a lot of (dynamic) categories at runtime, then it
is best to define a metaclass or a prototype with a predicate that will
call this built-in predicate in order to provide more sophisticated
behavior.</p>
</section>
<section id="abolishing-an-existing-category">
<span id="categories-abolishing"></span><h2>Abolishing an existing category<a class="headerlink" href="#abolishing-an-existing-category" title="Link to this heading"></a></h2>
<p>Dynamic categories can be abolished using the
<a class="reference internal" href="../refman/predicates/abolish_category_1.html#predicates-abolish-category-1"><span class="std std-ref">abolish_category/1</span></a> built-in predicate:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- abolish_category(Category).
</pre></div>
</div>
<p>The argument must be an identifier of a defined dynamic category;
otherwise, an error will be thrown.</p>
</section>
<section id="category-directives">
<span id="categories-directives"></span><h2>Category directives<a class="headerlink" href="#category-directives" title="Link to this heading"></a></h2>
<p>Category directives are used to define category properties, document
category dependencies on other Logtalk entities, and load the
contents of files into a category.</p>
<section id="dynamic-categories">
<span id="categories-dynamic"></span><h3>Dynamic categories<a class="headerlink" href="#dynamic-categories" title="Link to this heading"></a></h3>
<p>As usually happens with Prolog code, a category can be either static or
dynamic. A category created during the execution of a program is always
dynamic. A category defined in a file can be either dynamic or static.
Dynamic categories are declared by using the <a class="reference internal" href="../refman/directives/dynamic_0.html#directives-dynamic-0"><span class="std std-ref">dynamic/0</span></a>
directive in the category source code:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">dynamic</span>.
</pre></div>
</div>
<p>The directive must precede any predicate directives or clauses. Please
be aware that using dynamic code results in a performance hit when
compared to static code. We should only use dynamic categories when
these need to be abolished during program execution.</p>
</section>
<section id="category-documentation">
<span id="categories-documentation"></span><h3>Category documentation<a class="headerlink" href="#category-documentation" title="Link to this heading"></a></h3>
<p>A category can be documented with arbitrary user-defined information
by using the <a class="reference internal" href="../refman/directives/info_1.html#directives-info-1"><span class="std std-ref">info/1</span></a> entity directive. See the
<a class="reference internal" href="documenting.html#documenting-documenting"><span class="std std-ref">Documenting</span></a> section for details.</p>
</section>
<section id="loading-files-into-a-category">
<span id="categories-include"></span><h3>Loading files into a category<a class="headerlink" href="#loading-files-into-a-category" title="Link to this heading"></a></h3>
<p>The <a class="reference internal" href="../refman/directives/include_1.html#directives-include-1"><span class="std std-ref">include/1</span></a> directive
can be used to load the contents of a file into a category. See the
<a class="reference internal" href="objects.html#objects-objects"><span class="std std-ref">Objects</span></a> section for an example of using this
directive.</p>
</section>
<section id="declaring-object-aliases">
<span id="categories-object-aliases"></span><h3>Declaring object aliases<a class="headerlink" href="#declaring-object-aliases" title="Link to this heading"></a></h3>
<p>The <a class="reference internal" href="../refman/directives/uses_1.html#directives-uses-1"><span class="std std-ref">uses/1</span></a> directive can be used to declare object aliases.
The typical uses of this directive are to shorten long object names and to
simplify experimenting with different object implementations of the same
protocol when using explicit message-sending.</p>
</section>
</section>
<section id="category-relationships">
<span id="categories-relationships"></span><h2>Category relationships<a class="headerlink" href="#category-relationships" title="Link to this heading"></a></h2>
<p>Logtalk provides two sets of built-in predicates that enable us to query
the system about the relationships that a category has with other entities.</p>
<p>The built-in predicates <a class="reference internal" href="../refman/predicates/implements_protocol_2_3.html#predicates-implements-protocol-2-3"><span class="std std-ref">implements_protocol/2-3</span></a>
and <a class="reference internal" href="../refman/predicates/conforms_to_protocol_2_3.html#predicates-conforms-to-protocol-2-3"><span class="std std-ref">conforms_to_protocol/2-3</span></a>
allow us to find which categories implement which protocols:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- implements_protocol(Category, Protocol, Scope).
</pre></div>
</div>
<p>or, if we also want to consider inherited protocols:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- conforms_to_protocol(Category, Protocol, Scope).
</pre></div>
</div>
<p>Note that, if we use an unbound first argument, we will need to use the
<a class="reference internal" href="../refman/predicates/current_category_1.html#predicates-current-category-1"><span class="std std-ref">current_category/1</span></a> built-in predicate to ensure that
the returned entity is a category and not an object.</p>
<p>To find which objects import which categories, we can use the
<a class="reference internal" href="../refman/predicates/imports_category_2_3.html#predicates-imports-category-2-3"><span class="std std-ref">imports_category/2-3</span></a> built-in predicates:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- imports_category(Object, Category).
</pre></div>
</div>
<p>or, if we also want to know the importation scope:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- imports_category(Object, Category, Scope).
</pre></div>
</div>
<p>Note that a category may be imported by several objects.</p>
<p>To find which categories extend other categories, we can use the
<a class="reference internal" href="../refman/predicates/extends_category_2_3.html#predicates-extends-category-2-3"><span class="std std-ref">extends_category/2-3</span></a> built-in predicates:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- extends_category(Category1, Category2).
</pre></div>
</div>
<p>or, if we also want to know the extension scope:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- extends_category(Category1, Category2, Scope).
</pre></div>
</div>
<p>Note that a category may be extended by several categories.</p>
<p>To find which categories explicitly complement existing objects we can
use the <a class="reference internal" href="../refman/predicates/complements_object_2.html#predicates-complements-object-2"><span class="std std-ref">complements_object/2</span></a> built-in predicate:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- complements_object(Category, Object).
</pre></div>
</div>
<p>Note that a category may explicitly complement several objects.</p>
</section>
<section id="category-properties">
<span id="categories-properties"></span><h2>Category properties<a class="headerlink" href="#category-properties" title="Link to this heading"></a></h2>
<p>We can find the properties of defined categories by calling the built-in
predicate <a class="reference internal" href="../refman/predicates/category_property_2.html#predicates-category-property-2"><span class="std std-ref">category_property/2</span></a>:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- category_property(Category, Property).
</pre></div>
</div>
<p>The following category properties are supported:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">static</span></code></dt><dd><p>The category is static</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">dynamic</span></code></dt><dd><p>The category is dynamic (and thus can be abolished in runtime by
calling the <a class="reference internal" href="../refman/predicates/abolish_category_1.html#predicates-abolish-category-1"><span class="std std-ref">abolish_category/1</span></a> built-in predicate)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">built_in</span></code></dt><dd><p>The category is a built-in category (and thus always available)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">file(Path)</span></code></dt><dd><p>Absolute path of the source file defining the category (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">file(Basename,</span> <span class="pre">Directory)</span></code></dt><dd><p>Basename and directory of the source file defining the category (if
applicable); <code class="docutils literal notranslate"><span class="pre">Directory</span></code> always ends with a <code class="docutils literal notranslate"><span class="pre">/</span></code></p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">lines(BeginLine,</span> <span class="pre">EndLine)</span></code></dt><dd><p>Source file begin and end lines of the category definition (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">directive(BeginLine,</span> <span class="pre">EndLine)</span></code></dt><dd><p>Source file begin and end lines of the category opening directive (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">events</span></code></dt><dd><p>Messages sent from the category generate events</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">source_data</span></code></dt><dd><p>Source data available for the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">public(Resources)</span></code></dt><dd><p>List of public predicates and operators declared by the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">protected(Resources)</span></code></dt><dd><p>List of protected predicates and operators declared by the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">private(Resources)</span></code></dt><dd><p>List of private predicates and operators declared by the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">declares(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for a predicate declared by the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">defines(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for a predicate defined by the category</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">includes(Predicate,</span> <span class="pre">Entity,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for an object multifile predicate that are defined
in the specified entity (the properties include
<code class="docutils literal notranslate"><span class="pre">number_of_clauses(Number)</span></code>, <code class="docutils literal notranslate"><span class="pre">number_of_rules(Number)</span></code>,
<code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the
first multifile predicate clause)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">provides(Predicate,</span> <span class="pre">Entity,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for other entity multifile predicate that are
defined in the category (the properties include
<code class="docutils literal notranslate"><span class="pre">number_of_clauses(Number)</span></code>, <code class="docutils literal notranslate"><span class="pre">number_of_rules(Number)</span></code>,
<code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the
first multifile predicate clause)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">alias(Entity,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for an <a class="reference internal" href="../glossary.html#term-entity-alias"><span class="xref std std-term">entity alias</span></a> declared by the object
(the properties include <code class="docutils literal notranslate"><span class="pre">object</span></code> in case of an object alias, <code class="docutils literal notranslate"><span class="pre">module</span></code> in case of a module alias,
<code class="docutils literal notranslate"><span class="pre">for(Original)</span></code>, <code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the <code class="docutils literal notranslate"><span class="pre">uses/1</span></code> or
<code class="docutils literal notranslate"><span class="pre">use_module/1</span></code> directive)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">alias(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for a <a class="reference internal" href="../glossary.html#term-predicate-alias"><span class="xref std std-term">predicate alias</span></a> declared by the category
(the properties include <code class="docutils literal notranslate"><span class="pre">predicate</span></code>, <code class="docutils literal notranslate"><span class="pre">for(Original)</span></code>, <code class="docutils literal notranslate"><span class="pre">from(Entity)</span></code>, <code class="docutils literal notranslate"><span class="pre">non_terminal(NonTerminal)</span></code>,
<code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the alias directive)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">calls(Call,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for predicate calls made by the category (<code class="docutils literal notranslate"><span class="pre">Call</span></code>
is either a predicate indicator or a control construct such as
<code class="docutils literal notranslate"><span class="pre">(::)/1-2</span></code> or <code class="docutils literal notranslate"><span class="pre">(^^)/1</span></code> with a predicate indicator as argument; note
that <code class="docutils literal notranslate"><span class="pre">Call</span></code> may not be ground in case of a call to a control
construct where its argument is only know at runtime; the properties
include <code class="docutils literal notranslate"><span class="pre">caller(Caller)</span></code>, <code class="docutils literal notranslate"><span class="pre">alias(Alias)</span></code>, <code class="docutils literal notranslate"><span class="pre">non_terminal(NonTerminal)</span></code>,
<code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Caller</span></code>, <code class="docutils literal notranslate"><span class="pre">Alias</span></code>, and <code class="docutils literal notranslate"><span class="pre">NonTerminal</span></code>
being predicate indicators and <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the
predicate clause or directive making the call)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">updates(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for dynamic predicate updates (and also access
using the <code class="docutils literal notranslate"><span class="pre">clause/2</span></code> predicate) made by the object (<code class="docutils literal notranslate"><span class="pre">Predicate</span></code>
is either a predicate indicator or a control construct such as
<code class="docutils literal notranslate"><span class="pre">(::)/1-2</span></code> or <code class="docutils literal notranslate"><span class="pre">(:)/2</span></code> with a predicate indicator as argument; note
that <code class="docutils literal notranslate"><span class="pre">Predicate</span></code> may not be ground in case of a control construct
argument only know at runtime; the properties include
<code class="docutils literal notranslate"><span class="pre">updater(Updater)</span></code>, <code class="docutils literal notranslate"><span class="pre">alias(Alias)</span></code>, <code class="docutils literal notranslate"><span class="pre">non_terminal(NonTerminal)</span></code>,
<code class="docutils literal notranslate"><span class="pre">lines(Start,End)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Start)</span></code> with <code class="docutils literal notranslate"><span class="pre">Updater</span></code> being a (possibly multifile)
predicate indicator, <code class="docutils literal notranslate"><span class="pre">Alias</span></code> and <code class="docutils literal notranslate"><span class="pre">NonTerminal</span></code> being predicate
indicators, and <code class="docutils literal notranslate"><span class="pre">Start</span></code> being the begin line of the predicate clause
or directive updating the predicate)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">number_of_clauses(Number)</span></code></dt><dd><p>Total number of predicate clauses defined in the category (includes
both user-defined clauses and auxiliary clauses generated by the
compiler or by the <a class="reference internal" href="expansion.html#expansion-expansion"><span class="std std-ref">expansion hooks</span></a> but
does not include clauses for multifile predicates defined for other
entities or clauses for the category own multifile predicates
contributed by other entities)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">number_of_rules(Number)</span></code></dt><dd><p>Total number of predicate rules defined in the category (includes
both user-defined rules and auxiliary rules generated by the compiler
or by the <a class="reference internal" href="expansion.html#expansion-expansion"><span class="std std-ref">expansion hooks</span></a> but does not
include rules for multifile predicates defined for other entities or
rules for the category own multifile predicates contributed by other
entities)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">number_of_user_clauses(Number)</span></code></dt><dd><p>Total number of user-defined predicate clauses defined in the
category (does not include clauses for multifile predicates defined
for other entities or clauses for the category own multifile predicates
contributed by other entities)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">number_of_user_rules(Number)</span></code></dt><dd><p>Total number of user-defined predicate rules defined in the category
(does not include rules for multifile predicates defined for other
entities or rules for the category own multifile predicates contributed
by other entities)</p>
</dd>
</dl>
<p>Some properties, such as line numbers, are only available when the category is
defined in a source file compiled with the <a class="reference internal" href="programming.html#flag-source-data"><span class="std std-ref">source_data</span></a>
flag turned on. Moreover, line numbers are only supported in
<a class="reference internal" href="../glossary.html#term-backend-Prolog-compiler"><span class="xref std std-term">backend Prolog compilers</span></a>
that provide access to the start line of a read term. When such support is
not available, the value <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned for the start and end lines.</p>
<p>The properties that return the number of clauses (rules) report the
clauses (rules) <em>textually defined in the object</em> for both multifile and
non-multifile predicates. Thus, these numbers exclude clauses (rules)
for multifile predicates <em>contributed</em> by other entities.</p>
</section>
<section id="importing-categories">
<span id="categories-importing"></span><h2>Importing categories<a class="headerlink" href="#importing-categories" title="Link to this heading"></a></h2>
<p>Any number of objects can import a category. In addition, an object may
import any number of categories. The syntax is very simple:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">imports</span>([<span class="nv">Category1</span>, <span class="nv">Category2</span>, ...])).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>To make all public predicates imported via a category protected, or to
make all public and protected predicates private, we prefix the
category’s name with the corresponding keyword:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">imports</span>(private<span class="o">::</span><span class="nv">Category</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>or:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">imports</span>(protected<span class="o">::</span><span class="nv">Category</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>Omitting the scope keyword is equivalent to writing:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">imports</span>(public<span class="o">::</span><span class="nv">Category</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
</section>
<section id="calling-category-predicates">
<span id="categories-predicates"></span><h2>Calling category predicates<a class="headerlink" href="#calling-category-predicates" title="Link to this heading"></a></h2>
<p>Category predicates can be called from within an object by sending a
message to <em>self</em> or using a <em>super</em> call. Consider the following
category:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(output).

<span class="p">    :- </span><span class="k">public</span>(out<span class="o">/</span><span class="m">1</span>).

    out(<span class="nv">X</span>) <span class="o">:-</span>
        <span class="k">write</span>(<span class="nv">X</span>), <span class="k">nl</span>.

<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>The predicate <code class="docutils literal notranslate"><span class="pre">out/1</span></code> can be called from within an object importing
the category by simply sending a message to <em>self</em>. For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(worker,
    <span class="k">imports</span>(output)).

    ...
    do(<span class="nv">Task</span>) <span class="o">:-</span>
        execute(<span class="nv">Task</span>, <span class="nv">Result</span>),
        <span class="o">::</span>out(<span class="nv">Result</span>).
    ...

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>This is the recommended way of calling a category predicate that can be
specialized/overridden in a descendant object, as the predicate definition
lookup will start from <em>self</em>.</p>
<p>A direct call to a predicate definition found in an imported category can
be made using the <a class="reference internal" href="../refman/control/call_super_1.html#control-call-super-1"><span class="std std-ref">(^^)/1</span></a>
control construct. For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(worker,
    <span class="k">imports</span>(output)).

    ...
    do(<span class="nv">Task</span>) <span class="o">:-</span>
        execute(<span class="nv">Task</span>, <span class="nv">Result</span>),
        <span class="o">^^</span>out(<span class="nv">Result</span>).
    ...

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>This alternative should only be used when the user knows a priori that
the category predicates will not be specialized or redefined by
descendant objects of the object importing the category. Its advantage
is that, when the <a class="reference internal" href="programming.html#flag-optimize"><span class="std std-ref">optimize</span></a> flag is turned
on, the Logtalk compiler will try to optimize the calls by using
<a class="reference internal" href="../glossary.html#term-static-binding"><span class="xref std std-term">static binding</span></a>. When <a class="reference internal" href="../glossary.html#term-dynamic-binding"><span class="xref std std-term">dynamic binding</span></a> is used due to e.g.
the lack of sufficient information at compilation time, the performance is
similar to calling the category predicate using a message to <a class="reference internal" href="../glossary.html#term-self"><span class="xref std std-term">self</span></a>
(in both cases, a predicate lookup caching mechanism is used).</p>
</section>
<section id="parametric-categories">
<span id="categories-parametric"></span><h2>Parametric categories<a class="headerlink" href="#parametric-categories" title="Link to this heading"></a></h2>
<p>Category predicates can be parameterized in the same way as object predicates
by using a compound term as the category identifier where all the arguments
of the compound term are variables. These variables, the <em>category parameters</em>,
can be accessed by calling the <a class="reference internal" href="../refman/methods/parameter_2.html#methods-parameter-2"><span class="std std-ref">parameter/2</span></a> or
<a class="reference internal" href="../refman/methods/this_1.html#methods-this-1"><span class="std std-ref">this/1</span></a> built-in local methods in the category predicate
clauses or by using <a class="reference internal" href="../glossary.html#term-parameter-variable"><span class="xref std std-term">parameter variables</span></a>.</p>
<p>Category parameter values can be defined by the importing objects.
For example:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(speech(<span class="nv">Season</span>, <span class="nv">Event</span>),
    <span class="k">imports</span>([dress(<span class="nv">Season</span>), speech(<span class="nv">Event</span>)])).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>Note that access to category parameters is only possible from within the
category. In particular, calls to the <a class="reference internal" href="../refman/methods/this_1.html#methods-this-1"><span class="std std-ref">this/1</span></a> built-in local
method from category predicates always access the importing object identifier
(and thus object parameters, not category parameters).</p>
</section>
<section id="built-in-categories">
<span id="categories-built-in"></span><h2>Built-in categories<a class="headerlink" href="#built-in-categories" title="Link to this heading"></a></h2>
<p>Logtalk defines a built-in category that is always available for any
application.</p>
<section id="the-built-in-category-core-messages">
<span id="categories-core-messages"></span><h3>The built-in category <code class="docutils literal notranslate"><span class="pre">core_messages</span></code><a class="headerlink" href="#the-built-in-category-core-messages" title="Link to this heading"></a></h3>
<p>The built-in <a class="reference external" href="../../apis/core_messages_0.html#core-messages-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">core_messages</span></a> category provides
default translations for all compiler and runtime printed messages, such as
warnings and errors. It does not define any public predicates.</p>
</section>
</section>
<section id="multi-threading-applications">
<h2>Multi-threading applications<a class="headerlink" href="#multi-threading-applications" title="Link to this heading"></a></h2>
<p>When writing multi-threading applications, user-defined predicates calling
built-in predicates such as <code class="docutils literal notranslate"><span class="pre">create_category/4</span></code> and <code class="docutils literal notranslate"><span class="pre">abolish_category/1</span></code>
may need to be declared synchronized in order to avoid race conditions.</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="protocols.html" class="btn btn-neutral float-left" title="Protocols" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="predicates.html" class="btn btn-neutral float-right" title="Predicates" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>